using System.Data.Common;
using System.Data;
using System.IO;
using UnityEngine;
using System.Collections;
using System.Collections.Generic; // required for Dictionary<>, List<>, etc.

public class GUIController : MonoBehaviour {

	private List<GameItem> itemCollection = null;
	private GameDatabase gameDatabase = null;
	private DTORepository<GameItem> itemRepository = null;
	
	// Use this for initialization
	void Start () {		
				
		// Usually logic to set up a db connection and any associated objects is done in an application
		// startup routine, because you want it done once, and not during gameplay.
		// For this example, GUIController is the only class which the user interacts with, so
		// db initialization and setup is done here, and in the Start() method, so it's done prior to user interaction.
		
		// a collection to hold all game objects of type GameItem
		itemCollection = new List<GameItem>();

		// initialize database
		gameDatabase = ScriptableObject.CreateInstance<GameDatabase>();
		gameDatabase.SetDefaultConnection();
		
		// set up the item repository, which the gui will need to instantiate GameItems using db data
		IDTODbLogic<GameItem> gameItemDbLogic = new GameItemDbLogic();			
		itemRepository = DTORepository<GameItem>.GetInstance(gameItemDbLogic);
	}

	// Update is called once per frame
	void Update () {
	
	}
	
	void OnGUI () {
		int left = 10;
		int top = 10;
		int width = 400;
		int labelHeight = 20;
		int padding = 5;
		int buttonHeight = labelHeight * 2;
				
		if (GUI.Button (new Rect (left, top, width, buttonHeight), "Click to load inventory")) {							
			itemCollection = itemRepository.FindAll(gameDatabase);
		}
		
		if (itemCollection.Count == 0) { return; }
		
		Debug.Log ("Making labels for " + itemCollection.Count + " item(s).");
		
		// we've got some inventory, so display it
		top += buttonHeight + padding;		
		GUI.Label(new Rect(left, top, width, labelHeight), "We have the following items:");		

		// iterate through the item collection, creating a label to display each one
		foreach (GameItem item in itemCollection) {

			string inventoryDescr = 
				string.Format("Game Item ID:{0} is a(n): {1}, with property Can Heal = {2}",
							  item.GameItemId, item.GameItemType.ItemTypeDescription, item.GameItemType.CanHeal);

			top += labelHeight + padding;
			GUI.Label(new Rect(left, top, width, labelHeight), inventoryDescr);			
		}		
	}
}
